﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Server.Base;
using ZhimeiSoft.CharmManager.Server.DAL.Interface;


namespace ZhimeiSoft.CharmManager.Server.DAL
{
    public class EmployeeDAL : BaseDal, IEmployeeDAL
    {
        /// <summary>
        /// 设置SQL语句中的参数
        /// </summary>
        /// <param name="cmd">DbCommand对象</param>
        /// <param name="employee">员工实体类</param>
        private void AddInSqlParameters(ref DbCommand cmd, EmployeeEntity employee)
        {
            if (employee == null || cmd == null)
                return;

            DB.AddInParameter(cmd, "EMPLOYEEID", DbType.Int32, employee.EmployeeID);
            DB.AddInParameter(cmd, "ENAME", DbType.String, employee.EName);
            DB.AddInParameter(cmd, "EADDRESS", DbType.String, employee.EAddress);
            DB.AddInParameter(cmd, "EBIRTHDAY", DbType.DateTime, employee.EBirthday);
            DB.AddInParameter(cmd, "ECARD", DbType.String, employee.ECard);
            DB.AddInParameter(cmd, "ECODE", DbType.String, employee.ECode);
            DB.AddInParameter(cmd, "EEDUCATION", DbType.String, employee.EEducation);
            DB.AddInParameter(cmd, "EENTRYDATE", DbType.DateTime, employee.EEntryDate);
            DB.AddInParameter(cmd, "EFAMILYPHONE", DbType.String, employee.EFamilyPhone);
            DB.AddInParameter(cmd, "EINDEXCODE", DbType.String, employee.EIndexCode);
            DB.AddInParameter(cmd, "EMOBILEPHONE", DbType.String, employee.EMobilePhone);
            DB.AddInParameter(cmd, "EPASSWORD", DbType.String, employee.EPassword);
            DB.AddInParameter(cmd, "EQUITDATE", DbType.DateTime, employee.EQuitDate);
            DB.AddInParameter(cmd, "ESALONID", DbType.Int32, employee.ESalonID);
            DB.AddInParameter(cmd, "ESTATE", DbType.String, employee.EState);
        }

        /// <summary>
        /// 新增员工信息
        /// </summary>
        /// <param name="employee">员工信息实体类</param>
        /// <returns>员工ID</returns>
        public void InsertEmployee(EmployeeEntity employee)
        {
            string strSQL = EmployeeDAL_Const.SQL_INSERT_EMPLOYEE;
            DbCommand cmdSelect = DB.GetSqlStringCommand(strSQL);

            employee.EmployeeID = this.GetMaxID("EMPLOYEEINFO", "EMPLOYEEID") + 1;
            AddInSqlParameters(ref cmdSelect, employee);

            DB.ExecuteNonQuery(cmdSelect);
        }

        /// <summary>
        /// 更新员工信息
        /// </summary>
        /// <param name="employee">员工信息实体类</param>
        public void UpdateEmployee(EmployeeEntity employee)
        {
            string strSQL = EmployeeDAL_Const.SQL_UPDATE_EMPLOYEE;
            DbCommand cmdUpdate = DB.GetSqlStringCommand(strSQL);
            AddInSqlParameters(ref cmdUpdate, employee);
            DB.ExecuteNonQuery(cmdUpdate);
        }

        /// <summary>
        /// 删除员工信息
        /// </summary>
        /// <param name="employeeid">员工ID</param>
        public void DeleteEmployee(int employeeid)
        {
            string strSQL = EmployeeDAL_Const.SQL_DELETE_EMPLOYEE;
            DbCommand cmdDelete = DB.GetSqlStringCommand(strSQL);
            DB.AddInParameter(cmdDelete, "EMPLOYEEID", DbType.Int32, employeeid);
            DB.ExecuteNonQuery(cmdDelete);
        }
        /// <summary>
        /// 根据状态查询员工记录
        /// </summary>
        /// <param name="state">状态</param>
        /// <returns>返回数据</returns>
        public DataSet QueryEmployeeState(string state)
        {
            DbCommand queryComm = DB.GetSqlStringCommand(EmployeeDAL_Const.SQL_QUERY_EMPLOYEE_STATE);
            DB.AddInParameter(queryComm, "ESTATE", DbType.String, state);
            return DB.ExecuteDataSet(queryComm);
        }
        
    }
}
